package com.xxx.blog.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component    //将该类实例化到IOC容器中
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response
            , Object handler) throws Exception {
        //获取session，判断session是否有用户信息
        Object user=request.getSession().getAttribute("user");
        if (user==null){
            //用户未登录,进行重定向
            //log.debug("未登录请求"+request.getRequestURI());
            response.sendRedirect(request.getContextPath()+"/admin");
            return false;
        }
        //log.debug("放行登录请求"+request.getRequestURI());
        return true;
    }
}
